package com.sdy.dcsb.mq.consumer;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdy.dcsb.biz.constants.MqConstants;
import com.sdy.dcsb.biz.model.EsbAtomlog;
import com.sdy.dcsb.biz.model.EsbAtomlogReq;
import com.sdy.dcsb.biz.model.EsbLog;
import com.sdy.dcsb.biz.model.EsbLogReq;
import com.sdy.dcsb.biz.service.EsbAtomlogReqService;
import com.sdy.dcsb.biz.service.EsbAtomlogService;
import com.sdy.dcsb.biz.service.EsbLogReqService;
import com.sdy.dcsb.biz.service.EsbLogService;
import com.sdy.mq.base.BaseMessageConsumer;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.nio.charset.Charset;
import java.util.*;

@Slf4j
@Component
public class DcsbMessageConsumer extends BaseMessageConsumer {

    @Autowired
    private EsbAtomlogService esbAtomlogService;
    @Autowired
    private EsbAtomlogReqService esbAtomlogReqService;
    @Autowired
    private EsbLogService esbLogService;
    @Autowired
    private EsbLogReqService esbLogReqService;

    private static HashMap reqLogSplitMap = new HashMap();

    @Override
    protected boolean consume(MessageExt message) {
        String msgBody = "";
        JSONObject object = new JSONObject();
        try{
            msgBody = new String(message.getBody(), Charset.forName(RemotingHelper.DEFAULT_CHARSET));
            object = JSONObject.parseObject(msgBody);
        }catch (Exception e) {
            log.info(e.getMessage());
        }
        switch (message.getTags()) {
            case MqConstants.Tags.ESB_ATOMLOG:
                EsbAtomlog esbAtomlog = new EsbAtomlog();
                esbAtomlog.setDomainname(object.getString("domainname"));
                esbAtomlog.setAppname(object.getString("appname"));
                esbAtomlog.setReqid(object.getString("reqid"));
                esbAtomlog.setAtomexpid(object.getInteger("atomexpid"));
                esbAtomlog.setStatus(object.getString("status"));
                esbAtomlog.setRequestTime(object.getDate("request_time"));
                esbAtomlog.setResponseTime(object.getDate("response_time"));
                if(!esbAtomlogService.save(esbAtomlog)) {
                    log.info("mq时间：" + new Date() + "保存原子服务日志失败");
                }
                break;
            case MqConstants.Tags.ESB_ATOMLOG_REQ:
                EsbAtomlogReq esbAtomlogReq = new EsbAtomlogReq();
                esbAtomlogReq.setReqid(object.getString("reqid"));
                esbAtomlogReq.setAtomexpid(object.getInteger("atomexpid"));
                esbAtomlogReq.setInsoapmsg(object.getString("insoapmsg"));
                esbAtomlogReq.setOutsoapmsg(object.getString("outsoapmsg"));
                esbAtomlogReq.setRequestTime(object.getDate("request_time"));
                esbAtomlogReq.setResponseTime(object.getDate("response_time"));
                if(!esbAtomlogReqService.save(esbAtomlogReq)) {
                    log.info("mq时间：" + new Date() + "保存原子服务报文失败");
                }
                break;
            case MqConstants.Tags.ESB_LOG:
                EsbLog esbLog = new EsbLog();
                esbLog.setDomainname(object.getString("domainname"));
                esbLog.setAppname(object.getString("appname"));
                esbLog.setRequestip(object.getString("requestip"));
                esbLog.setReqid(object.getString("reqid"));
                esbLog.setExpid(object.getInteger("expid"));
                esbLog.setStatus(object.getString("status"));
                esbLog.setReqlength(object.getString("reqlength"));
                esbLog.setResplength(object.getString("resplength"));
                esbLog.setRequestTime(object.getDate("request_time"));
                esbLog.setResponseTime(object.getDate("response_time"));
                esbLog.setSystemCode(object.getString("system_code"));
                if(!esbLogService.save(esbLog)) {
                    log.info("mq时间：" + new Date() + "保存服务日志失败");
                }
                break;
            case MqConstants.Tags.ESB_LOG_REQ:
                EsbLogReq esbLogReq = new EsbLogReq();
                esbLogReq.setReqid(object.getString("reqid"));
                esbLogReq.setRequestTime(object.getDate("request_time"));
                esbLogReq.setResponseTime(object.getDate("response_time"));
                esbLogReq.setSystemCode(object.getString("system_code"));
                esbLogReq.setExpid(object.getInteger("expid"));
                esbLogReq.setServcode(object.getString("servcode"));
                esbLogReq.setInsoapmsg(object.getString("insoapmsg"));
                esbLogReq.setOutsoapmsg(object.getString("outsoapmsg"));
                esbLogReq.setVersion(object.getString("version"));
                esbLogReq.setQuerystring(object.getString("querystring"));
                esbLogReq.setReqtype(object.getString("reqtype"));
                if(!esbLogReqService.save(esbLogReq)) {
                    log.info("mq时间：" + new Date() + "保存服务报文失败");
                }
                break;
            case MqConstants.Tags.ESB_LOG_REQ_SPLIT:
                String reqid = object.getString("reqid");
                QueryWrapper<EsbLogReq> esbLogReqWrapper = new QueryWrapper<>();
                esbLogReqWrapper.eq("reqid", reqid);
                List<EsbLogReq> esbLogReqResult = esbLogReqService.list(esbLogReqWrapper);
                if(esbLogReqResult.size() == 0) {
                    return false;
                }else {
                    if(!reqLogSplitMap.containsKey(reqid)) {
                        if(object.containsKey("size")) {
                            reqLogSplitMap.put(reqid, object);
                        }else {
                            return false;
                        }
                    }else {
                        JSONObject mapJson = JSONObject.parseObject(reqLogSplitMap.get(reqid).toString());
                        Integer size = mapJson.getInteger("size");
                        Integer num = object.getInteger("num");
                        if((size - num) == 1) {
                            String outsoapmsg = object.getString("outsoapmsg") +
                                    mapJson.getString("outsoapmsg");
                            mapJson.put("outsoapmsg", outsoapmsg);
                            size--;
                            mapJson.put("size", size);
                            reqLogSplitMap.put(reqid, mapJson);
                            if(size == 1) {
                                EsbLogReq esbLogReqSplit = new EsbLogReq();
                                esbLogReqSplit.setOutsoapmsg(outsoapmsg);
                                QueryWrapper<EsbLogReq> esbLogReqQueryWrapper = new QueryWrapper<>();
                                esbLogReqQueryWrapper.eq("reqid", reqid);
                                esbLogReqService.update(esbLogReqSplit, esbLogReqQueryWrapper);
                                Iterator<Map.Entry<String, Object>> it = reqLogSplitMap.entrySet().iterator();
                                while(it.hasNext()) {
                                    Map.Entry<String, Object> item = it.next();
                                    if(reqid.equals(item.getKey())) {
                                        it.remove();
                                    }
                                }
                            }else {

                            }
                        }else {
                            return false;
                        }
                    }
                }
                break;
            case MqConstants.Tags.ESB_ATOMLOG_REQ_SPLIT:
                String atomReqid = object.getString("reqid");
                QueryWrapper<EsbAtomlogReq> esbAtomlogReqWrapper = new QueryWrapper<>();
                esbAtomlogReqWrapper.eq("reqid", atomReqid.substring(4));
                List<EsbAtomlogReq> esbAtomlogReqResult = esbAtomlogReqService.list(esbAtomlogReqWrapper);
                if(esbAtomlogReqResult.size() == 0) {
                    return false;
                }else {
                    if(!reqLogSplitMap.containsKey(atomReqid)) {
                        if(object.containsKey("size")) {
                            reqLogSplitMap.put(atomReqid, object);
                        }else {
                            return false;
                        }
                    }else {
                        JSONObject mapJson = JSONObject.parseObject(reqLogSplitMap.get(atomReqid).toString());
                        Integer size = mapJson.getInteger("size");
                        Integer num = object.getInteger("num");
                        if((size - num) == 1) {
                            String outsoapmsg = object.getString("outsoapmsg") +
                                    mapJson.getString("outsoapmsg");
                            mapJson.put("outsoapmsg", outsoapmsg);
                            size--;
                            mapJson.put("size", size);
                            reqLogSplitMap.put(atomReqid, mapJson);
                            if(size == 1) {
                                EsbAtomlogReq esbAtomlogReqSplit = new EsbAtomlogReq();
                                esbAtomlogReqSplit.setOutsoapmsg(outsoapmsg);
                                QueryWrapper<EsbAtomlogReq> esbAtomlogReqQueryWrapper = new QueryWrapper<>();
                                esbAtomlogReqQueryWrapper.eq("reqid", atomReqid.substring(4));
                                esbAtomlogReqService.update(esbAtomlogReqSplit, esbAtomlogReqQueryWrapper);
                                Iterator<Map.Entry<String, Object>> it = reqLogSplitMap.entrySet().iterator();
                                while(it.hasNext()) {
                                    Map.Entry<String, Object> item = it.next();
                                    if(atomReqid.equals(item.getKey())) {
                                        it.remove();
                                    }
                                }
                            }else {

                            }
                        }else {
                            return false;
                        }
                    }
                }
                break;
            default:
                break;
        }
        return true;
    }
}
